package com.ruoyi.mes.service.impl;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.mes.domain.*;
import com.ruoyi.mes.mapper.*;
import com.ruoyi.mes.utils.AvgUtils;
import com.ruoyi.mes.utils.LongMenUtils;
import com.ruoyi.mes.vo.device.AvgUDPVo;
import com.ruoyi.mes.vo.pick.PickDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mes.service.IPickInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 领料信息Service业务层处理
 *
 * @author LongAndHao
 * @date 2023-04-04
 */
@Service
public class PickInfoServiceImpl implements IPickInfoService {
    @Autowired
    private WkspTaskMapper wkspTaskMapper;

    @Autowired
    private MtrStockMapper mtrStockMapper;

    @Autowired
    private PickInfoMapper pickInfoMapper;

    @Autowired
    private HouInfoMapper houInfoMapper;

    @Autowired
    private LocatorInfoMapper locatorInfoMapper;

    /**
     * 查询领料信息
     *
     * @param id 领料信息主键
     * @return 领料信息
     */
    @Override
    public PickInfo selectPickInfoById(Long id) {
        return pickInfoMapper.selectPickInfoById(id);
    }

    /**
     * 查询领料信息列表
     *
     * @param pickInfo 领料信息
     * @return 领料信息
     */
    @Override
    public List<PickInfo> selectPickInfoList(PickInfo pickInfo) {
        return pickInfoMapper.selectPickInfoList(pickInfo);
    }

    /**
     * 新增领料信息
     *
     * @param pickInfo 领料信息
     * @return 结果
     */
    @Override
    public int insertPickInfo(PickInfo pickInfo) {
        pickInfo.setCreateTime(DateUtils.getNowDate());
        return pickInfoMapper.insertPickInfo(pickInfo);
    }

    /**
     * 修改领料信息
     *
     * @param pickInfo 领料信息
     * @return 结果
     */
    @Override
    public int updatePickInfo(PickInfo pickInfo) {
        pickInfo.setUpdateTime(DateUtils.getNowDate());
        return pickInfoMapper.updatePickInfo(pickInfo);
    }

    /**
     * 批量删除领料信息
     *
     * @param ids 需要删除的领料信息主键
     * @return 结果
     */
    @Override
    public int deletePickInfoByIds(Long[] ids) {
        return pickInfoMapper.deletePickInfoByIds(ids);
    }

    /**
     * 删除领料信息信息
     *
     * @param id 领料信息主键
     * @return 结果
     */
    @Override
    public int deletePickInfoById(Long id) {
        return pickInfoMapper.deletePickInfoById(id);
    }

    /**
     * 查 - 领料信息分页
     *
     * @param pickInfo
     * @return
     */
    @Override
    public List<PickInfo> pickInfoList(PickInfo pickInfo) {
        return pickInfoMapper.pickInfoList(pickInfo);
    }

    /**
     * 查 - 领料详情
     *
     * @param id 领料id
     * @return
     */
    @Override
    public AjaxResult getDetails(Long id) {
        PickDetailVo pickDetailVo = new PickDetailVo();

        PickInfo pickInfo = pickInfoMapper.selectPickInfoById(id);
        pickDetailVo.setPickInfo(pickInfo);


        //物料信息
        List<MtrInfo> mtrs = new ArrayList<>();
        //物料库存
        List<MtrStock> mtrStocks = new ArrayList<>();
        List<PickInfo> pickInfos = pickInfoMapper.selectPickInfoByPickCode(pickInfo.getPickCode());
        for (PickInfo info : pickInfos) {
            MtrInfo mtrInfo = new MtrInfo();
            mtrInfo.setMtrCode(info.getMtrCode());
            mtrInfo.setMtrQuantity(info.getPickQuantity());
            mtrInfo.setMtrName(info.getMtrName());
            mtrs.add(mtrInfo);

            List<MtrStock> stocks = mtrStockMapper.selectMtrStockByMtrCode(info.getMtrCode());
            if (stocks.size() !=0){//库存有相应物料才返回
                //查找库位
                for (MtrStock stock : stocks) {
                    List<LocatorInfo> locatorBySerialCode = locatorInfoMapper.getLocatorBySerialCode(stock.getMtrCode());
                    stock.setLocatorCode(locatorBySerialCode.get(0).getCode());
                    HouInfo houInfo = houInfoMapper.selectHouInfoByHouId(stock.getHouId());
                    stock.setHouName(houInfo.getHouName());
                }
                mtrStocks.add(stocks.stream().max(Comparator.comparing(MtrStock::getMtrQuantity)).get());
            }
        }
        pickDetailVo.setMtrStocks(mtrStocks);
        pickDetailVo.setMtrs(mtrs);

        //若缺少库存原料，则返回报错信息
        if (pickDetailVo.getMtrs().size() != pickDetailVo.getMtrStocks().size()){
            pickDetailVo.setError("缺少库存物料!");
        }

        return AjaxResult.success("查询成功", pickDetailVo);
    }


    /**
     * 原料出库
     *
     * @param id 领料id
     * @return
     */
    @Override
    @Transactional
    public AjaxResult outBound(Long id) {
        PickInfo pickInfo = pickInfoMapper.selectPickInfoById(id);
        List<PickInfo> pickInfos = pickInfoMapper.selectPickInfoByPickCode(pickInfo.getPickCode());

        for (PickInfo info : pickInfos) {
            List<MtrStock> stocks = mtrStockMapper.selectMtrStockByMtrCode(info.getMtrCode());
            MtrStock mtrStock = stocks.stream().max(Comparator.comparing(MtrStock::getMtrQuantity)).get();

            if (stocks.size() == 0){
                return AjaxResult.error("缺少库存物料！");
            }
            //判断库存，不充足直接退出
            if (mtrStock.getMtrQuantity() < info.getPickQuantity()) {
                return AjaxResult.error("库存数量不足!");
            }
            //调取龙门抓取动作
            LongMenUtils.LongMenGrab();
            //更新物料库存
            mtrStock.setMtrQuantity(mtrStock.getMtrQuantity() - info.getPickQuantity());
            mtrStockMapper.updateMtrStock(mtrStock);
            //更新库房剩余容量
            HouInfo houInfo = houInfoMapper.selectHouInfoByHouId(mtrStock.getHouId());
            if (houInfo.getHouRemainCapacity() + info.getPickQuantity() > houInfo.getHouCapacity()){
                houInfo.setHouRemainCapacity(houInfo.getHouCapacity());
            }
            houInfo.setHouRemainCapacity(houInfo.getHouRemainCapacity() + info.getPickQuantity());
            houInfoMapper.updateHouInfo(houInfo);

            //更新库位
            List<LocatorInfo> locatorBySerialCode = locatorInfoMapper.getLocatorBySerialCode(info.getMtrCode());
            LocatorInfo locatorInfo = locatorBySerialCode.get(0);
            locatorInfo.setRemainCapacity(locatorInfo.getRemainCapacity() + info.getPickQuantity());
            locatorInfoMapper.updateLocatorInfo(locatorInfo);

            //更新领料状态
            info.setIspicked(1L);
            pickInfoMapper.updatePickInfo(info);
            //更新车间任务的领料状态
            WkspTask wkspTask = wkspTaskMapper.selectWkspTaskById(info.getWkspTaskId());
            wkspTask.setPickingStatus(2L);
            wkspTaskMapper.updateWkspTask(wkspTask);
        }
        return AjaxResult.success("原料出库成功!");
    }

    /**
     * 删 - 领料出库
     * @param id 领料id
     * @return
     */
    @Override
    @Transactional
    public AjaxResult deletePicks(Long id) {
        PickInfo pickInfo = pickInfoMapper.selectPickInfoById(id);

        WkspTask wkspTask = wkspTaskMapper.selectWkspTaskById(pickInfo.getWkspTaskId());
        wkspTask.setPickingStatus(0L);//删除更新车间任务 - 未领料
        wkspTaskMapper.updateWkspTask(wkspTask);

        List<PickInfo> pickInfos = pickInfoMapper.selectPickInfoByPickCode(pickInfo.getPickCode());
        for (PickInfo info : pickInfos) {
            pickInfoMapper.deletePickInfoById(info.getId());
        }

        return AjaxResult.success("删除成功!");
    }

    /**
     * 派遣小车
     * @return
     */
    @Override
    public AjaxResult dispatchAvg(Long id) {
        //调取小车工具类
        AvgUtils.DispatchAvg();

        //派遣后要更改状态
        PickInfo pickInfo = pickInfoMapper.selectPickInfoById(id);
        List<PickInfo> pickInfos = pickInfoMapper.selectPickInfoByPickCode(pickInfo.getPickCode());
        for (PickInfo info : pickInfos) {
            info.setIsdispatch(1L);
            pickInfoMapper.updatePickInfo(info);
        }

        return AjaxResult.success("派遣成功");
    }
}
